package com.hetao.jmb2c.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hetao.jmb2c.common.PageResult;
import com.hetao.jmb2c.common.Result;
import com.hetao.jmb2c.dto.*;
import com.hetao.jmb2c.entity.Member;
import com.hetao.jmb2c.service.MemberService;
import com.hetao.jmb2c.vo.MemberPageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "会员管理", description = "会员信息管理接口")
@Slf4j
@RestController
@RequestMapping("/member")
public class MemberController {

    @Resource
    private MemberService memberService;

    /**
     * StringRedisTemplate是RedisTemplate的子类，专门处理String类型数据
     * 使用场景：
     * - 缓存会员信息
     * - 存储会话token
     * - 实现分布式锁
     */
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 批量删除会员
     * 
     * @param ids 要删除的会员ID列表
     * @return 操作结果
     * 
     * 初学者提示：
     * - @RequestBody 表示从请求体获取JSON数据
     * - removeBatchByIds是MyBatis Plus提供的批量删除方法
     * - 实际项目中应该添加权限验证
     */
    @Operation(summary = "批量删除会员", description = "根据ID列表批量删除会员信息")
    @DeleteMapping()
    public Result<Object> delete(
            @Parameter(description = "会员ID列表", required = true)
            @RequestBody List<Long> ids) {
        this.memberService.removeBatchByIds(ids);
        return Result.success();
    }

    @Operation(summary = "分页查询会员", description = "根据条件分页查询会员信息")
    @GetMapping("page")
    public PageResult<MemberPageVO> page(
            @Parameter(description = "分页查询参数", required = true)
            @ModelAttribute MemberPageQueryDTO memberPageQueryDTO) {
        IPage<MemberPageVO> page = this.memberService.getPage(memberPageQueryDTO);
        return PageResult.success(page);
    }

    @GetMapping("{id}")
    public Result<Member> get(@PathVariable Long id) {
        Member member = this.memberService.getById(id);
        return Result.success(member);
    }

    @Operation(summary = "编辑会员信息", description = "根据ID编辑会员信息")
    @PutMapping("")
    public Result<Object> update(@RequestBody Member member) {
        this.memberService.updateById(member);
        return Result.success();
    }

    @Operation(summary = "重置密码",description = "重置会员密码")
    @PutMapping("resetPassword/{id}")
    public Result<Object> resetPassword(@PathVariable Long id){
        this.memberService.resetPassword(id);
        return Result.success();
    }

    @Operation(summary = "启用禁用会员", description = "根据ID启用或禁用会员")
    @PutMapping("enable/{id}")
    public Result<Object> enable(@PathVariable Long id) {
        this.memberService.enable(id);
        return Result.success();
    }
}
